import event
import dispatcher
import pygame
import filter
import event_looper
from common import *

class KeyDownFilter(filter.Filter):
    def __init__(self):
        filter.Filter.__init__(self)
    def match(self, event_id):
        if event_id == event.KeyDownEvent.event_id:
            return True
        return False
    def handle(self, event):
        if D_KEY_MAPS not in event.extra_data:
            event.error_msg = '%s not in extra_data.' % D_KEY_MAPS
            event.set_state(event.ERROR_HAPPENED)
            return
        key_maps = event.extra_data[D_KEY_MAPS]
        pressed_key = [x for x in range(len(key_maps)) if key_maps[x]]
        for key in pressed_key:
            print pygame.key.name(key)

def test_main():
    pygame.init()
    
    pygame.display.set_mode((800,600))
    
    keydown_filter = KeyDownFilter()
    dispter = dispatcher.Dispatcher()
    dispter.attach(event.KeyDownEvent.event_id, keydown_filter)
    e_looper = event_looper.EventLooper(dispter)
    e_looper.start()

if __name__ == '__main__':
    test_main()